package com.reggie.configuration.AutoFill;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.reggie.configuration.common.LocalThread;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("插入数据");

        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());


    if (LocalThread.getEmployeeThread()!= null){
        if (LocalThread.getEmployeeThread().getId() != null){
            metaObject.setValue("createUser", LocalThread.getEmployeeThread().getId());
            metaObject.setValue("updateUser", LocalThread.getEmployeeThread().getId());

        }
    }

    if (LocalThread.getUserThread()!= null) {
        if (LocalThread.getUserThread().getId() != null) {
            metaObject.setValue("createUser", LocalThread.getUserThread().getId());
            metaObject.setValue("updateUser", LocalThread.getUserThread().getId());

        }
    }

}

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("修改数据");
        metaObject.setValue("updateTime",LocalDateTime.now());

        if (LocalThread.getUserThread() != null && LocalThread.getUserThread().getId() != null) {
            metaObject.setValue("updateUser", LocalThread.getUserThread().getId());
        }

        if(LocalThread.getEmployeeThread()!= null && LocalThread.getEmployeeThread().getId() != null){
            metaObject.setValue("updateUser", LocalThread.getEmployeeThread().getId());
        }
    }
}
